<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here
function top_header(){
    echo "<div align=\"right\"><table><tr>
        <td><a href=\"?mod=panel&file=users&action=add_user\">
        <img class=icon src=\"images/add_user.png\" title=\"Add New User\"/></a></td>
        <td><a href=\"?mod=panel&file=users&action=add_group\">
        <img class=icon src=\"images/add_group.png\" title=\"Add New Group\"/></a></td>
        <td><a href=\"?mod=panel&file=users&action=user_list\">
        <img class=icon src=\"images/user_list.png\" title=\"User List\"/></a></td>
        <td><a href=\"?mod=panel&file=users&action=group_list\">
        <img class=icon src=\"images/group_list.png\" title=\"Group List\"/></a></td>
        <td><a href=\"?mod=panel&file=users&action=privileges\">
        <img class=icon src=\"images/group_privs.png\" title=\"Group Privileges\"/></a> </td></tr></table></div>";
}
function add_form(){
    
}
function add_process(){
    
}
function edit_form(){
    
}
function edit_process(){
    
}
function delete(){
    
}
function list_table(){
    global $db;
    top_header();
    
}
function user_list(){
    global $db,$page,$pagesize,$login_status,$is_admin;
    if (empty($page)){
        $start=0;
    } else $start=$page*$pagesize;
    $sql="select * from _user_users order by user_name asc limit $start,$pagesize";
    $result=$db->query($sql);
    top_header();
    echo "<table width=\"100%\" cellspacing=0><tr><th>No</th><th>Nama User</th>
        <th>Nama Lengkap</th><th>User Email</th><th>Messenger</th><th>Action</th></tr>";
     $i=0;
    while ($row=$db->fetch_array($result)){
        extract($row);
         $i++;
        $pos=strpos($email,'@');
        $username=substr($email,0,$pos);
        echo "<tr><td>$i</td><td>$user_name</td><td>$fullname</td><td>$username</td>
        <td>$messenger : $messenger_id</td>
        <td><a href=\"?mod=users&action=chprofile&user_edit=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a href=\"?mod=panel&file=users&action=compose&id=$id\">
    <img class=icon src=\"images/ic_email.png\" title=\"Send Email\" /></a></td></tr>\r\n";
    }
    echo "</table>";
}
function group_list(){
    global $db,$page,$pagesize,$login_status,$is_admin;
    if (empty($page)){
        $start=0;
    } else $start=$page*$pagesize;
    $sql="select * from _user_groups order by group_name asc limit $start,$pagesize";
    $result=$db->query($sql);
    top_header();
    echo "<table width=\"100%\"  cellspacing=0><tr><th>No</th><th>Nama Group</th>
        <th>Keterangan</th> <th>Action</th></tr>";
    $i=0;
    while ($row=$db->fetch_array($result)){
        extract($row);
        $i++; 
        echo "<tr><td>$i</td><td>$group_name</td><td>$group_desc</td> 
        <td align=center><a href=\"?mod=panel&file=users&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a href=\"?mod=panel&file=users&action=compose&id=$id\">
    <img class=icon src=\"images/ic_email.png\" title=\"Send Email\" /></a></td></tr>\r\n";
    }
    echo "</table>";
}
function add_group(){
    echo "<form action=\"?mod=panel&file=users\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"add_group_process\" /><table>
<tr><td colspan=2><h2>Add New Group</h2></td></tr>
<tr><td>Group Name</td><td><input type=\"text\" name=\"group_name\" class=\"textbox\"/> </td></tr>
<tr><td>Group Desc</td><td><textarea cols=\"50\" rows=5 name=\"group_desc\" class=\"textbox\"></textarea> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Add New Group\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function add_group_process(){
    global $db,$group_name,$group_desc;
    if (!empty($group_name)){
        $sql="insert into _user_groups values (null,'$group_name','$group_desc')";
        $db->query($sql);
        refresh_to("?mod=panel&file=users&action=group_list", "Anda dialihkan ke Control panel User List", 2);
    }
}
function compose(){
    global $id;
    $user_email=get_user_info_id($id,'email');
    ?>
<form action="?mod=panel&file=users" method="post">
    
    <table>
        <tr><td>Subject</td><td><input type="text" name="subject" size="50"/></td></tr>
        <tr><td>To</td><td><input type="text" name="email_to" size="50" value="<?php echo $user_email;?>"/></td></tr>
        <tr><td>Message Body</td><td>
                <textarea name="email_message" cols="50" rows="10" rel="editor"></textarea>
            </td></tr>
        <tr><td></td><td><input type="submit" value="Sent"/></td></tr>
            
    </table>
    
</form>
<?php
}
function send_message(){
    
}
switch($action){
    case 'add_process':
        add_process();
        break;
    case 'add_user':
        add_form();
        break;
    case 'edit_user':
        edit_form();
        break;
    case 'edit_user_process':
        edit_process();
        break;
    case 'add_group':
        add_group();
        break;
    case 'add_group_process':
        add_group_process();
        break;
    case 'user_list':
        user_list();
        break;
    case 'group_list':
        group_list();
        break;
    case 'privileges':
        privileges();
        break;
    case 'compose':
        compose();
        break;
    default:
        list_table();
        break;
}
?>